package com.dearbinge.opentapi.content.im;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.im.UserContactTagService;
import com.dearbinge.data.pojo.im.ImUserContactTag;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 标签控制层
 *
 * @author wes
 * @date 2019/2/27
 */
@Api(tags = "5.3、通讯-好友标签")
@RestController
@RequestMapping("/userContactTag")
@CrossOrigin
@EnableAutoConfiguration
public class UserContactTagController extends WebMvcConfigurerAdapter {

    @Autowired
    private UserContactTagService userContactTagService;

    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query",required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id) {


        try {
            Integer dataCode = userContactTagService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody ImUserContactTag userContactTag){
        try {
            if (userContactTag.getId() != null && userContactTag.getId() != 0){
                Integer dataCode = userContactTagService.update(userContactTag);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else{
                Integer dataCode = userContactTagService.insert(userContactTag);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询对象",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<ImUserContactTag> getById(Integer id){
        try {
            ImUserContactTag userContactTag = userContactTagService.getById(id);
            return ResultMsg.ok(userContactTag);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据用户ID查询所有标签
     */
    @ApiOperation(value = "根据ID查询全部",notes = "根据ID查询")
    @ApiImplicitParam(name = "userId",value = "userId",paramType = "query")
    @RequestMapping(value = "/listByUserId",method = RequestMethod.GET)
    public ResultMsg<Page> listByUserId(Integer userId,Integer current, Integer size){
        try {
            Page page = userContactTagService.listByUserId(userId, current,  size);
            return ResultMsg.ok(page);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }




}
